<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
class manage extends admin{
	private $db;
	private $admin_db;
	
	public $site;
	
	function __construct() {
		pc_base::load_app_class('global');
		pc_base::load_app_func('submenu','experience');//加载检测履历分类下是否存在子类
		$this->config = pc_base::load_config('database');//加载数据库配置
		$this->talent_db = pc_base::load_model('talent_model');//加载人才model
		$this->db = pc_base::load_model('experience_model');//加载履历模型
		$this->experience_type_db = pc_base::load_model('experience_type_model');//加载履历类型模型
		$this->admin_db = pc_base::load_model('admin_model');//加载后台用户模型
		$this->site = get_siteid();//获取当前站点
		$this->admin_id = param::get_cookie('userid');//获取后台管理员ＩＤ
		parent::__construct();
	}
	
	public function init() {
		include $this->admin_tpl('experience_list');
	}
	
	public function add(){
		if (isset($_POST['dosubmit'])) {
			$info = isset($_POST['experience']) ? $_POST['experience'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$talent_id = $_POST['talent_id'];
			$new_experience = array();
			//遍历info销毁空数据
			foreach ($info as $k=>$v) {
				if (empty($v) || $v == '') {
					unset($info[$k]);
				}
				$new_experience = $info;
			}
			$sql = "";
			$sql .= "insert into ".$this->config['default']['tablepre']."experience (talent_id,experience_id,content,create_time,update_time,created_by,updated_by,siteid,status) values ";
			foreach ($new_experience as $k=>$v){
				$sql .= "('".$talent_id."','".$k."','".$v."','".date('Y-m-d H:i:s')."','".date('Y-m-d H:i:s')."','".$this->admin_id."','".$this->admin_id."','".$this->site."','1'),";
			}
			$sql = trim($sql,',');
			$result = $this->db->query($sql);
			if ($result) {
				showmessage(L('add_experience_success'),'?m=resource_asso&c=manage&a=moter_record&source_type=4&talent_id='.$talent_id);
			} else {
				showmessage(L('add_experience_error'),HTTP_REFERER);
			}
		} else {
			$condition = "";
			$condition .= "siteid = ".$this->site." and status =1";
			$experience_type = $this->experience_type_db->listinfo($condition,'id ASC');
			$is_experience = $this->db->select(array('talent_id'=>$_GET['talent_id'],'siteid'=>$this->site,'status'=>1),'experience_id');
			$is_experience_arr = array();
			foreach ($is_experience as $k=>$v) {
				$is_experience_arr[$k] = $v['experience_id'];
			}
			
			$experience_arr = array();
			foreach ($experience_type as $k=>$v) {
				if (in_array($v['id'], $is_experience_arr)) {
					unset($experience_type[$k]);
				}
				$experience_arr = check_submenu($v['id']);
				if ($experience_arr == true) {
					unset($experience_type[$k]);
				}
			}
			$show_header = false;
			pc_base::load_sys_class('form','',0);
			include $this->admin_tpl('experience_add');
		}
	}
	
	public function edit() {
		$id = isset($_GET['id']) ? $_GET['id'] : showmessage(L('illegal_parameters'),HTTP_REFERER);//履历ID
		$talent_id = $_GET['talent_id'];//人才ID
		if (isset($_POST['dosubmit'])) {
			$info = isset($_POST['experience']) ? $_POST['experience'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$data['content'] = $info['content'];
			$data['update_time'] = date('Y-m-d H:i:s');
			$result = $this->db->update($data,array('id'=>$id));
			if($result) {
				showmessage(L('edit_experience_success'),'?m=resource_asso&c=manage&a=moter_record&source_type=4&talent_id='.$_POST['talent_id']);
			} else {
				showmessage(L('edit_experience_error'),HTTP_REFERER);
			}
		} else {
			//查询履历名称、内容
			$sql = "";
			$sql .= "select a.*,b.name,en_name from nsrweb_experience as a left join nsrweb_experience_type as b on a.experience_id = b.id where a.id = ".$id." and talent_id = ".$talent_id;
			$info = $this->db->my_listinfo($sql);
			foreach ($info as $k=>$v){
				$new_info = $v;
			}
			pc_base::load_sys_class('form','',0);
			include $this->admin_tpl('experience_edit');
		}
	}
	
	public function del(){
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('message not exits'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		$talent_id = $_REQUEST['talent_id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$result = $this->db->update(array(
				'status' => '2'
		), "id in ('$id')");
		if ($result) {
			showmessage(L('del_experience_success'), '?m=resource_asso&c=manage&a=moter_record&source_type=4&talent_id='.$talent_id);
		} else {
			showmessage(L('del_experience_error'), HTTP_REFERER);
		}
	}
	
	/**
	* 检测履历分类下是否存在子类
	* @date: 2017年3月21日 上午10:11:42
	* @author: LIYANG
	*/
	public function check_submenu($parentid) {
		if (isset($parentid)) {
			$result = $this->experience_type_db->get_one(array('parent_id'=>$parentid));
			if ($result) {
				return true;
			} else {
				return false;
			}
		}
	}
}